<?php
    session_start();
    include_once '../conexion/Conexion10.php';
    $con = new Conexion10();
    
    $doc = $_REQUEST['documento'];
    
    $select = " SELECT ADNINGRESO.AINFECING,ADNINGRESO.OID as oidingreso,ADNINGRESO.AINCONSEC,GENPACIEN.OID,PACPRINOM,PACSEGNOM,PACPRIAPE,PACSEGAPE,CAST(DATEDIFF(DD, GENPACIEN.GPAFECNAC, GETDATE()) / 365.25 AS INT) AS EDAD,
                           GDENOMBRE, PACTELEFONO,GENDETCON.OID as oide,GENTERCER.TERNUMDOC
                from GENPACIEN INNER JOIN ADNINGRESO ON GENPACIEN.OID=ADNINGRESO.GENPACIEN
                                                                   LEFT OUTER JOIN GENDETCON ON ADNINGRESO.GENDETCON=GENDETCON.OID
                                                                   LEFT OUTER JOIN SLNFACTUR ON ADNINGRESO.OID = SLNFACTUR.ADNINGRESO 
                                                                   LEFT OUTER JOIN GENTERCER ON GENDETCON.GENTERCER1 = GENTERCER.OID
                                           LEFT OUTER JOIN GENPACIENT ON GENPACIENT.GENPACIEN = GENPACIEN.OID
                WHERE GENPACIEN.PACNUMDOC = '".$doc."'
                ORDER BY AINFECING DESC";
    $res = $con->query($select);
    
    while($res2 = mssql_fetch_array($res)){
        $listas[] = array_map(utf8_encode,$res2); 
    }
    
    print_r(json_encode($listas));
    
    $con->cerrarConexion();
?>